package com.dev.utils;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.IDataAbilityObserver;
import ohos.agp.window.dialog.ToastDialog;
import ohos.sysappcomponents.settings.SystemSettings;

/**
 * 检测屏幕亮度工具类
 */
public class ScreenUtils {

    //屏幕监听相关
    private static DataAbilityHelper dataAbilityHelper;
    private static IDataAbilityObserver dataAbilityObserver;

    /**
     * 注册屏幕亮度监听
     *
     * @param slice
     */
    public static void subscribeScreen(AbilitySlice slice) {
        dataAbilityHelper = DataAbilityHelper.creator(slice);
        dataAbilityObserver = new IDataAbilityObserver() {
            int tag = 0;
            @Override
            public void onChange() {
                if (tag == 0) {
                    SystemSettings.getValue(dataAbilityHelper, SystemSettings.Display.SCREEN_BRIGHTNESS_STATUS);
                    new ToastDialog(slice).setText("屏幕亮度改变！").show();
                    LogUtils.info("SCREEN", "wifi is on!");
                    tag++;
                }
            }
        };
        dataAbilityHelper.registerObserver(SystemSettings.getUri(SystemSettings.Display.SCREEN_BRIGHTNESS_STATUS)
                , dataAbilityObserver);
    }

    public static void unSubscribeScreen() {
        dataAbilityHelper.unregisterObserver(SystemSettings.getUri(SystemSettings.Display.SCREEN_BRIGHTNESS_STATUS)
                , dataAbilityObserver);

    }
}
